package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.log.DouLuoDaLuBattleReportData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DouLuoDaLuBattleReportDataDaoImpl extends TempDataObjectDao<DouLuoDaLuBattleReportData> {

    public static String getDouLuoDaLuBattleReportDataSql = "select * from `t_u_douluodalu_battle_report` where `activityId`=? AND `userId` = ? order by `logTime` desc limit 50";
    public List<DouLuoDaLuBattleReportData> getDouLuoDaLuBattleReportData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DouLuoDaLuBattleReportData> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuBattleReportDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                result.add(parse(rs)) ;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDouLuoDaLuBattleReportDataSql = "insert into `t_u_douluodalu_battle_report`(activityId,userId,enemyBaseInfo,enemyId,patronsId,battleType,patronsNum,`allKill`,score,logTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuBattleReportData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAllKill()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuBattleReportDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuBattleReportDataSql = "update `t_u_douluodalu_battle_report` set userId=?,enemyBaseInfo=?,enemyId=?,patronsId=?,battleType=?,patronsNum=?,`allKill`=?,score=?,logTime=? where activityId=?";

    @Override
    protected boolean updateData(DouLuoDaLuBattleReportData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBattleType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAllKill()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuBattleReportDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuBattleReportData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuBattleReportData data = new DouLuoDaLuBattleReportData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setEnemyBaseInfo(JSON.parseObject(rs.getString("enemyBaseInfo"), UserBaseInfo.class));
        data.setEnemyId(rs.getLong("enemyId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setBattleType(rs.getInt("battleType"));
        data.setPatronsNum(rs.getInt("patronsNum"));
        data.setAllKill(rs.getBoolean("allKill"));
        data.setScore(rs.getInt("score"));
        data.setLogTime(rs.getLong("logTime"));

        return data;
    }
}
